/*TXZpGO1PWbXe+mW0HDLWZoxPaOTjSACwW0ql57r6tj5RJ1r/idR2OgE+qe34VeXe*/
import { ajax, getBusinessInfo, toast } from 'nc-lightapp-front';
import { setFormEditable } from '../../../public/components/pubUtils/billPubUtil.js';

/**
 * 重新渲染某个字段的下拉区域
 * @param {*} that 
 * @param {*} props 
 * @param {*} areaid 
 * @param {*} attrcode 
 * @param {*} values 
 */
function renderSelectField(that, props, areaid, attrcode, values) {
    let meta = props.meta.getMeta();
    let faOptions = [];
    let showObj = {}
    if (values instanceof Array) {
        for (let i = 0; i < values.length; i++) {
            let value = values[i];
            let obj1 = getObject(that, value)
            faOptions.push(obj1)
            if( i == 0 ){
                showObj = obj1
            }
        }
    } else {
        let obj2 = getObject(that, values)
        faOptions.push(obj2)
        showObj = obj2
    }
    meta[areaid].items.map((item) => {
        if (item.attrcode == attrcode) {
            item.itemtype = 'select'
            item.options = faOptions;
        }
        return item;
    })
    let attrvalue = props.form.getFormItemsValue('cytjArea', attrcode).value
    // if(values.indexOf(attrvalue) == -1){
        var map = {};
        var key = attrcode;
        map[key] = showObj;
        if(areaid.indexOf("auto") != -1 ){
            props.form.setFormItemsValue('cytjArea_auto', map)
        }else {
            props.form.setFormItemsValue('cytjArea', map)
        }
    // }
    props.renderItem('form', areaid, attrcode, null);
    props.meta.setMeta(meta)
}

/**
 * 核销常用条件中设置所有子区域的可编辑性
 * @param {*} props 
 * @param {*} parentCode 
 * @param {*} arr 
 * @param {*} editable 
 */
function setAllFieldEditable(props, parentCode, arr, editable){
    if(arr != null && arr instanceof Array){
        for (let i = 0; i < arr.length; i++) {
            setFormEditable(props, parentCode, arr[i], editable);
        }
    }
}

/**
 * 应收核销常用条件中本方、对方根据数据控制字段可编辑性
 * @param {*} props 
 * @param {*} cytjCode 
 */
function setFieldDisabledAR(props,cytjCode){
	//设置本方单据类型	对方单据类型不可编辑
	let pk_org = props.form.getFormItemsValue(cytjCode, 'pk_org').value
	let jfhxdx = props.form.getFormItemsValue(cytjCode, 'jf_hxdxCBox').value
	let jfBilltype = props.form.getFormItemsValue(cytjCode, 'jfBillTypeBox').value
	let dfhxdx = props.form.getFormItemsValue(cytjCode, 'df_hxdxCBox').value
	let dfBilltype = props.form.getFormItemsValue(cytjCode, 'dfBillTypeBox').value

	props.form.setFormItemsDisabled(cytjCode, { jf_hxdxCBox: false,jfBillTypeBox: false,df_hxdxCBox: false,dfBillTypeBox: false });

	if(jfhxdx == 'kh' && jfBilltype == 'khys' && dfhxdx == 'gys' && dfBilltype == 'gysyf'){
		props.form.setFormItemsDisabled(cytjCode, { jf_hxdxCBox:true, jfBillTypeBox: true, dfBillTypeBox: true});
	}else if(jfhxdx == 'kh' && jfBilltype == 'khys' && dfhxdx == 'bm' && dfBilltype == 'khsk'){
		props.form.setFormItemsDisabled(cytjCode, { jfBillTypeBox:true, dfBillTypeBox: true});
	}else if(jfhxdx == 'kh' && jfBilltype == 'khys' && dfhxdx == 'ywy' && dfBilltype == 'khsk'){
		props.form.setFormItemsDisabled(cytjCode, { jfBillTypeBox:true, dfBillTypeBox: true});
	}else if(jfhxdx == 'kh' && jfBilltype == 'gysfk' && dfhxdx == 'kh' && dfBilltype == 'khsk'){
		props.form.setFormItemsDisabled(cytjCode, { dfBillTypeBox: true});
	}else if(jfhxdx == 'kh' && jfBilltype == 'gysfk' && dfhxdx == 'gys' && dfBilltype == 'khsk'){
		props.form.setFormItemsDisabled(cytjCode, { dfBillTypeBox: true});
	}else if(jfhxdx == 'gys' && jfBilltype == 'khys' && dfhxdx == 'kh' && dfBilltype == 'gysyf'){
		props.form.setFormItemsDisabled(cytjCode, { dfBillTypeBox: true});
	}else if(jfhxdx == 'gys' && jfBilltype == 'gysfk' && dfhxdx == 'gys' && dfBilltype == 'khsk'){
		props.form.setFormItemsDisabled(cytjCode, { dfBillTypeBox: true});
	}else if(jfhxdx == 'gys' && jfBilltype == 'gysfk' && dfhxdx == 'kh' && dfBilltype == 'khsk'){
		props.form.setFormItemsDisabled(cytjCode, { dfBillTypeBox: true});
	}
}

/**
 * 应付核销常用条件中本方、对方根据数据控制字段可编辑性
 * @param {*} props 
 * @param {*} cytjCode 
 */
function setFieldDisabledAP(props,cytjCode){
	//设置本方单据类型	对方单据类型不可编辑
	let pk_org = props.form.getFormItemsValue(cytjCode, 'pk_org').value
	let jfhxdx = props.form.getFormItemsValue(cytjCode, 'jf_hxdxCBox').value
	let jfBilltype = props.form.getFormItemsValue(cytjCode, 'jfBillTypeBox').value
	let dfhxdx = props.form.getFormItemsValue(cytjCode, 'df_hxdxCBox').value
	let dfBilltype = props.form.getFormItemsValue(cytjCode, 'dfBillTypeBox').value

	props.form.setFormItemsDisabled(cytjCode, { jf_hxdxCBox: false,jfBillTypeBox: false,df_hxdxCBox: false,dfBillTypeBox: false });

	if(jfhxdx == 'kh' && jfBilltype == 'gysfk' && dfhxdx == 'gys' && dfBilltype == 'khsk'){
		props.form.setFormItemsDisabled(cytjCode, { dfBillTypeBox: true});
	} else if(jfhxdx == 'kh' && jfBilltype == 'khys' && dfhxdx == 'kh' && dfBilltype == 'gysyf'){
		props.form.setFormItemsDisabled(cytjCode, { dfBillTypeBox: true});
	} else if(jfhxdx == 'kh' && jfBilltype == 'khys' && dfhxdx == 'gys' && dfBilltype == 'gysyf'){
		props.form.setFormItemsDisabled(cytjCode, { dfBillTypeBox: true});
	} else if(jfhxdx == 'gys' && jfBilltype == 'gysfk' && dfhxdx == 'kh' && dfBilltype == 'khsk'){
		props.form.setFormItemsDisabled(cytjCode, { jf_hxdxCBox:true, jfBillTypeBox: true, dfBillTypeBox: true});
	} else if(jfhxdx == 'gys' && jfBilltype == 'gysfk' && dfhxdx == 'bm' && dfBilltype == 'gysyf'){
		props.form.setFormItemsDisabled(cytjCode, { jfBillTypeBox: true, dfBillTypeBox: true});
	} else if(jfhxdx == 'gys' && jfBilltype == 'gysfk' && dfhxdx == 'ywy' && dfBilltype == 'gysyf'){
		props.form.setFormItemsDisabled(cytjCode, { jfBillTypeBox: true, dfBillTypeBox: true});
	} else if(jfhxdx == 'gys' && jfBilltype == 'khys' && dfhxdx == 'gys' && dfBilltype == 'gysyf'){
		props.form.setFormItemsDisabled(cytjCode, { dfBillTypeBox: true});
	} else if(jfhxdx == 'gys' && jfBilltype == 'khys' && dfhxdx == 'kh' && dfBilltype == 'gysyf'){
		props.form.setFormItemsDisabled(cytjCode, { dfBillTypeBox: true});
	}

}

//加载核销方案和默认币种
function loadVerifyFa(props, cytjCode, hxgzCode, value, funCode) {
	let meta = props.meta.getMeta();
	ajax({
		url: '/nccloud/arap/verify/loadVerifyFa.do',
		data: {
			funCode: funCode,
			pk_org: value.value
		},
		success: function (res) {
			let faOptions = [];
			faOptions.push({ display: "", value: null })
			let obj = {};
			res.data['verifyfa'].map((item) => {
				obj = {
					display: item.name,
					value: item.pk_verify_fa
				}
				faOptions.push(obj);
			})
			meta[cytjCode].items.map((item) => {
				if (item.attrcode == 'verifyFa') {
					item.itemtype = 'select'
					item.options = faOptions;
				}
				return item;
			})
			props.renderItem('form', cytjCode, 'verifyFa', null);
			let hxbz = res.data['currtype'];
			props.form.setFormItemsValue(cytjCode, { 'verifyFa': { value: null, display: null } },);
			props.form.setFormItemsValue(hxgzCode, { 'hxbzRef': { value: hxbz.value, display: hxbz.name } });
		}
	});
}

function writeBack(that, res, props, moduleId, cytjCode, cytjCode_auto, bfCode, dfCode, publicCode, hxgzCode, hxfsCode) {
	if (res.data[0]) {
		props.form.EmptyAllFormValue(cytjCode)
		props.form.setAllFormValue({ [cytjCode]: res.data[0][cytjCode] })
		//设置方案名称、本方单据类型、对方单据类型不可编辑
		props.form.setFormItemsDisabled(cytjCode, { name: true });
		props.form.setFormItemsDisabled(cytjCode, { jfBillTypeBox: true });
		props.form.setFormItemsDisabled(cytjCode, { dfBillTypeBox: true });
		//根据本方核销对象设置本方对象名称参照
		let meta = props.meta.getMeta()
		let jf_hxdxCBox = props.form.getFormItemsValue(cytjCode, 'jf_hxdxCBox').value;
		let df_hxdxCBox = props.form.getFormItemsValue(cytjCode, 'df_hxdxCBox').value;
		setRefCodeByHxdx(that, props, meta, jf_hxdxCBox, 'jf_dxmcRef', bfCode);
		setRefCodeByHxdx(that, props, meta, df_hxdxCBox, 'df_dxmcRef', dfCode);
		let date = getBusinessInfo().businessDate.substring(0, 10)
		let beginDate = date
		let endDate = date
		// 本方开始日期	本方结束日期
		props.form.setFormItemsValue(cytjCode, { 'jfBeginDateRef': { value: beginDate }, 'jfEndDateRef': { value: endDate } });
		// 对方开始日期	对方结束日期
		props.form.setFormItemsValue(cytjCode, { 'dfBeginDateRef': { value: beginDate }, 'dfEndDateRef': { value: endDate } });

	}
	if (res.data[1]) {
		props.form.EmptyAllFormValue(hxgzCode)
		props.form.setAllFormValue({ [hxgzCode]: res.data[1][hxgzCode] })
		props.form.setFormItemsValue(hxgzCode, { 'yxwcTxtF': { value: null, display: null } });
		props.form.setFormItemsValue(hxgzCode, { 'jfbz_dzjbzhlTxtF': { value: null, display: null } });
		props.form.setFormItemsValue(hxgzCode, { 'dfbz_dzjbzhlTxtF': { value: null, display: null } });
		//设置金额方向不可编辑
		let hldcChkBox = props.form.getFormItemsValue(hxgzCode, 'hldcChkBox');
		if (hldcChkBox.value == true) {
			props.form.setFormItemsDisabled(hxgzCode, { jefxCmbBox: true });
		}
	}
	if (res.data[2] && moduleId == cytjCode_auto) {
		props.form.EmptyAllFormValue(hxfsCode)
		props.form.setAllFormValue({ [hxfsCode]: res.data[2][hxfsCode] })
	}
}

function setRefCodeByHxdx(that, props, meta, hxdxValue, dxmcField, areaCode) {
	if (hxdxValue == 'kh') {
		meta[areaCode].items.map((item) => {
			if (item.attrcode == dxmcField) {
				item.itemtype = 'refer'
				item.refName = that.state.json['verificationsheet-000038']/* 国际化处理： 客户档案*/
				item.refcode = 'uapbd/refer/customer/CustomerDefaultTreeGridRef/index'
			}
			return item;
		})
	} else if (hxdxValue == 'gys') {
		meta[areaCode].items.map((item) => {
			if (item.attrcode == dxmcField) {
				item.itemtype = 'refer'
				item.refName = that.state.json['verificationsheet-000039']/* 国际化处理： 供应商档案*/
				item.refcode = 'uapbd/refer/supplier/SupplierRefTreeGridRef/index'
			}
			return item;
		})
	} else if (hxdxValue == 'bm') {
		meta[areaCode].items.map((item) => {
			if (item.attrcode == dxmcField) {
				item.itemtype = 'refer'
				item.refName = that.state.json['verificationsheet-000002']/* 国际化处理： 部门*/
				item.refcode = 'uapbd/refer/org/DeptTreeRef/index'
			}
			return item;
		})
	} else if (hxdxValue == 'ywy') {
		meta[areaCode].items.map((item) => {
			if (item.attrcode == dxmcField) {
				item.itemtype = 'refer'
				item.refName = that.state.json['verificationsheet-000042']/* 国际化处理： 人员*/
				item.refcode = 'uapbd/refer/psninfo/PsndocTreeGridRef/index'
			}
			return item;
		})
	}
	//重新渲染本方对象名称、对方对象名称
	props.renderItem('form', areaCode, dxmcField, null);
}

function getObject(that, value) {
    let display = '';
    if ('khys' == value) {
        display = that.state.json['verificationsheet-000040']/* 国际化处理： 应收单*/
    } else if ('khsk' == value) {
        display = that.state.json['verificationsheet-000044']/* 国际化处理： 收款单*/
    } else if ('gysyf' == value) {
        display = that.state.json['verificationsheet-000041']/* 国际化处理： 应付单*/
    } else if ('gysfk' == value) {
        display = that.state.json['verificationsheet-000043']/* 国际化处理： 付款单*/
    } else if ('kh' == value){
        display = that.state.json['verificationsheet-000000']/* 国际化处理： 客户*/
    } else if ('gys' == value){
        display = that.state.json['verificationsheet-000001']/* 国际化处理： 供应商*/
    } else if ('bm' == value){
        display = that.state.json['verificationsheet-000002']/* 国际化处理： 部门*/
    } else if ('ywy' == value){
        display = that.state.json['verificationsheet-000003']/* 国际化处理： 业务员*/
    }
    let obj = {
        display: display,
        value: value
    }
    return obj
}

export { renderSelectField, setAllFieldEditable, setFieldDisabledAR, setFieldDisabledAP, loadVerifyFa, writeBack }
/*TXZpGO1PWbXe+mW0HDLWZoxPaOTjSACwW0ql57r6tj5RJ1r/idR2OgE+qe34VeXe*/